﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using STF.OrangeDog.IService;
using STF.OrangeDog.IRepository;
using STF.OrangeDog.Dtos;
using STF.OrangeDog.Model;
using System.Threading.Tasks;

namespace STF.OrangeDog.Service
{
    public class PublishFormBll:IPublishFormBll
    {
        private IPublishFormDal _dal;

        public PublishFormBll(IPublishFormDal db)
        {
            _dal = db;
        }
        public async Task<int> AddReleaseFormAsync(string Title, string Details)
        {
            return await _dal.AddModelAsync(new Model.PublishForm
            {
                title = Title,
                details = Details
            });
        }

        public async Task<int> DeleteReleaseFormAsync(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            if (data.DeleteId == 1)
                return 0;
            return await _dal.RemoveModelAsync(data);
        }

        public async Task<int> EditReleaseFormAsync(int Id, string Title, string Details)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            if (data == null)
                return -1;
            data.title = Title;
            data.details = Details;
            data.UpdateTime = DateTime.Now;
            return await _dal.EditModelAsync(data);
        }

        public async Task<IEnumerable<PublishFormDtos>> GetAll(int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId);
            return data != null ? data.Select(x => new PublishFormDtos
            {
                
                title = x.title,
                details = x.details,
              
            })
                : new List<PublishFormDtos>();
        }

        public async Task<PublishFormDtos> GetDataById(int Id)
        {
            var data = await _dal.GetDataByIdAsync(Id);
            return data == null ? new PublishFormDtos() : new PublishFormDtos
            {
               
                title = data.title,
                details = data.details,
                
            };
        }

        public async Task<IEnumerable<PublishFormDtos>> GetDataByTitle(string Title, int DeleteId = 1)
        {
            var data = await _dal.GetDataByChooseAsync(r => r.DeleteId == DeleteId && r.title.Contains(Title));
            return data == null ? new List<PublishFormDtos>()
                : data.Select(x => new PublishFormDtos
                {  
                  
                    title = x.title,
                    details = x.details,
                   
                });
        }
    }
}
